package com.elione.modbus;

/**
 * MODBUS TCP slave information data type class
 * 
 * @author     Zhang hongyu 
 *
 * @version    1.0 (06.04.2005)
 * 
 * @version    1.1 (12.04.2005)
 * - new property retry_interval
 * 
 * @version    1.2 (22.07.2007)
 * - add new paramter to configure the socket timeout value 
 * 
 * @version    2.0 (15.10.2010 zha)
 * - add method setSlaveIP
 * 
 */

public class ModbusSlaveInfo { 
  /**
   * Create a ModbusSlaveInfo with default setting. 
   * (port=502,retry = 3,transacationID =0,protocolID = 0,reconnect = true,retry_interval = 20 )
   * @since   1.0
   *
   * @param   ip     the slave ip address (e.g. 192.168.100.1)
   */  
  public ModbusSlaveInfo(String ip) {
    slaveip    = ip;  
    tcp_port   = 502;
    retry_num  = 3;
    transactionID = 0;
    protocolID    =0;
    unitID     = 0;
    reconnect = true;
    retry_interval = 20;
    socketTimeout = 0; 
  }
  
  /**
   * Create a N7ModbusTcpSlaveInfo with given setting.
   *
   * @since   1.0
   *
   * @param   ip     the slave ip address (e.g. 192.168.100.1)
   * @param   port   the tcp port for MODBUS TCP connection 
   * @param   retry  the retry number in case of lost connection 
   */     
  public ModbusSlaveInfo(String ip, int port, int retry ) {
    slaveip    = ip;  
    tcp_port   = port;
    retry_num  = retry;
    transactionID = 0;
    protocolID    =0;  
    unitID     = 0;
    reconnect = true;
  }

  /**
   * set Tcp port for MODBUS TCP connection 
   *
   * @since   1.0
   * @param   port   the tcp port for MODBUS TCP connection 
   */     
  public void setTcpPort(int port ) {
    tcp_port = port;
  }  
  

  /**
   * set Transaction ID for MODBUS TCP connection 
   *
   * @since   1.0
   * @param   tranID   Transaction ID for MODBUS TCP connection, 
   *                   suppose to be positive value
   */       
  public void setTransactionID( int tranID) {
    transactionID = tranID;
  }


  /**
   * set protocol ID for MODBUS TCP connection 
   *
   * @since   1.0
   * @param   proID   Protocol ID for MODBUS TCP connection, 
   *                  suppose to be positive value
   */           
  public void setProtocolID (int proID) {
   protocolID = proID;
  }
  

  /**
   * set unit ID for MODBUS TCP connection 
   *
   * @since   1.0
   * @param   unitID  Unit ID for MODBUS TCP connection, 
   *                  suppose to be positive value
   */           
  public void setUnitID(int unitid) {
    unitID = unitid;
  }
  /**
   * set retry number 
   *
   * @since   1.0
   * @param   retry  the retry number in case of lost connection 
   */       
  public void setRetryNum(int retry) {
    retry_num = retry;
  }
  
  /**
   * set reconnect flag. True input means reset connection of this 
   * slave after each request.   
   *
   * @since   1.0
   * @param   retry  the retry number in case of lost connection 
   */       
  
  public void setReconnect(boolean b) {
    reconnect = b;
  }

  /**
   * @param slaveip the slaveip to set
   */
  public void setSlaveip(String slaveip) {
        this.slaveip = slaveip;
    }

  public String getSlaveIP() {
    return (slaveip);
  }
  
  public int getTcpPort() {
    return (tcp_port);
  }
  
  public int getRetryNum() {
    return (retry_num);
  }
  
  public int getTransactionID() {
    return (transactionID);
  }
  
  public int getProtocolID() {
    return (protocolID);
  }
  
  public int getUnitID() {
    return (unitID);
  }
  
  public boolean getReconnect() {
    return (reconnect);
  }
  /**
   * @return Returns the retry_interval.
   */
  public int getRetry_interval() {
      return retry_interval;
  }
  /**
   * @param retry_interval The retry_interval to set.
   */
  public void setRetry_interval(int retry_interval) {
    this.retry_interval = retry_interval;
  }
  

  /**
   * @return the socketTimeout
   */
  public int getSocketTimeout() {
      return socketTimeout;
  }

  /**
   * @param socketTimeout the socketTimeout to set
   */
  public void setSocketTimeout(int socketTimeout) {
      this.socketTimeout = socketTimeout;
  }
    
  
  /**
   * Return true if given objec is equal to this ModbusSlaveInfo object 
   * 
   *
   * @since   1.0
   * @return   
   */     
  public boolean equals (Object obj) {
    boolean equal = true;
    if (! (obj instanceof ModbusSlaveInfo) ) {
      equal = false;
    }
    if (! (slaveip.equals(((ModbusSlaveInfo)obj).slaveip)) ) {
      equal = false;
    }    
    if (! (tcp_port == ((ModbusSlaveInfo)obj).tcp_port) ) {
      equal = false;
    }
        
    if (! (retry_num == ((ModbusSlaveInfo)obj).retry_num) ) {
      equal = false;
    }
    
    if (! (transactionID == ((ModbusSlaveInfo)obj).transactionID) ) {
      equal = false;
    }
    
    if (! (protocolID == ((ModbusSlaveInfo)obj).protocolID) ) {
      equal = false;
    }    

    if (! (unitID == ((ModbusSlaveInfo)obj).unitID) ) {
      equal = false;
    }
    
    if (! (retry_interval == ((ModbusSlaveInfo)obj).retry_interval) ) {
        equal = false;
      }  
    return (equal);
  }
  
  
  /**
   * Return string as  [slaveip]-[tcp_port]-[retry_num]-[transactionID]-
   * [protocolID]+[unitID]+[retry_interval]+[reconnect]
   *
   * @since   1.0
   * @return   
   */   
  public String toString() {
    String info = slaveip + "-";
    info += tcp_port + "-";
    info += retry_num + "-";
    info += transactionID + "-";
    info += protocolID +"-" ;
    info += unitID + "_" ;
    info += retry_interval + "_" ;
    info += reconnect;
    return(info);
  }

  private String slaveip;        // slave ip address 
  private int tcp_port;          // slave tcp port for modbus tcp 
  private int retry_num;         // retry number in case of lost connection  
  private int transactionID;     // set transcationID 
  private int protocolID;        // protocol ID   
  private int unitID;            // unit ID
  private boolean reconnect;     // reconnect to slave after each request
  private int retry_interval;    // retry interval in ms
  
  private int socketTimeout;     // TCP socket timeout value 

  
}